import 'dart:math';

import 'package:flutter/material.dart';

class RoomList extends StatefulWidget {
  RoomList({super.key, required this.point, required this.building});

  String point;
  String building;

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  // 1. 定义初始数据
  Map data = {
    'point': '', // 小区信息
    'building': '', // 楼栋信息
    'rooms': [], // 随机产生的房间号
  };

  @override
  void initState() {
    super.initState();
    fakeData();
  }

  fakeData() {
    setState(() {
      data['point'] = widget.point;
      data['building'] = widget.building;
      // 随机产生8条数据
      Random r = Random();
      List rooms = [];
      for (var i = 0; i < 10; i++) {
        num temp1 = r.nextInt(12) + 1;
        num temp2 = r.nextInt(19) + 1;
        String level =
            temp1 > 10 ? temp1.toString() : temp1.toString().padLeft(2, '0');
        String number =
            temp2 > 10 ? temp2.toString() : temp2.toString().padLeft(2, '0');
        String value = '$level$number';

        if (rooms.contains(value)) continue;

        rooms.add(value);
      }
      rooms.sort((a, b) => b.compareTo(a));
      data['rooms'] = rooms;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('选择房间'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                Navigator.pushNamed(context, '/houseFrom', arguments: {
                  'houseInfo': {
                    'point': data['point'],
                    'building': data['building'],
                    'room': data['rooms'][index]
                  }
                });
              },
              child: Container(
                  color: Colors.white,
                  padding: const EdgeInsets.all(10),
                  child: Row(children: [
                    Expanded(
                        child: Text(
                            '${data['point']}${data['building']}${data['rooms'][index]}室')),
                    const Row(children: [
                      Icon(Icons.arrow_forward_ios,
                          size: 16, color: Colors.black),
                    ])
                  ])),
            );
          },
          itemCount: (data['rooms'] as List).length,
        ));
  }
}
